package com.pm.main.common;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author admin
 * @date 2020/11/2
 **/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PageRsp<T> {

    private int totalPages;
    private long totalElements;
    private List<T> data;

    public static <T> PageRsp<T> empty() {
        return new PageRsp<>(0, 0L, Collections.emptyList());
    }

    public <R> PageRsp<R> map(Function<T, R> function) {
        List<R> list = data.stream().map(function).collect(Collectors.toList());
        return new PageRsp<>(totalPages, totalElements, list);
    }

    public static <T> PageRsp<T> of(Long count, int size, List<T> data) {
        int totalPages = size == 0 ? 1 : (int) Math.ceil((double) count / (double) size);
        return new PageRsp<>(totalPages, count, data);
    }
}
